<script setup>
import {reactive, ref, watchEffect} from 'vue';
import {ElMessage} from "element-plus";
import * as api from "@/api/public.js";

// form表单类============
let form = reactive({
    id: '',
    code: '',
    name: '',
    level: 0,
    parentCode: '',
    firstLetter: ''
})

const formSetter = (v) => {
    for (let key in v) {
        form[key] = v[key];
    }
};
// ==============

const treeData = ref([])

const rules = reactive({
    name:[
      { required: true, message: '请输入菜单名称！', trigger: 'blur' }
    ],
    code:[
      { required: true, message: '请输入路由地址！', trigger: 'blur' }
    ],
    level:[
      { required: true, message: '请输入组件路径！', trigger: 'blur' }
    ]
})

const props = defineProps({
    data: Object
})

const visible = defineModel();

const convertor = (data) => {
    if (!data) return [];
    return data.map(item => {
        return {
              value: item.code,
              label: item.name,
              children: item.children ? convertor(item.children) : []
        }
    })
}

const transformTree = (data) =>{
    return [{
        label: "主类目",
        value: '0',
        children: convertor(data)
    }]
}

watchEffect(() => {
    treeData.value = transformTree(props.data?.treeData);
    formSetter(props.data)
})

const emit = defineEmits(['transfer','success'])

// 提交方法
const submit = (v) =>{
  delete form.treeData
  let url = '/area'
  if(form.status !== 1){
        form.id = ''
        api.add(form,url).then(res =>{
            ElMessage({
                message: res.msg,
                type: 'success'
            });
            emit('success')
            visible.value = false
        })
  }else{
    form.parentCode = form.copyParentCode
    api.update(form,url).then(res =>{
          ElMessage({
              message: res.msg,
              type: 'success'
          });
          emit('success')
          visible.value = false
      })
  }
}
</script>
<template>
    <div>
        <el-dialog class="dialog" v-model="visible" :title="form.status === 1 ? '修改' : '添加'" width="600">
            <el-form
                :model="form"
                :rules="rules"
                label-position="right"
            >
              <el-form-item label="上级地区">
                <el-tree-select
                    v-model="form.parentCode"
                    :data="treeData"
                    check-strictly
                />
              </el-form-item>
              <div class="formRow">
                <el-form-item label="地区名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入地区名称"></el-input>
                </el-form-item>
                <el-form-item label="首字母">
                  <el-input v-model="form.firstLetter" placeholder="请输入首字母"/>
                </el-form-item>
              </div>
              <div class="formRow">
              <el-form-item label="地区编码" prop="code">
                  <el-input v-model="form.code" placeholder="请输入地区编码"></el-input>
              </el-form-item>
                <el-form-item label="地区级别" prop="level">
                  <el-input-number
                      v-model="form.level"
                      controls-position="right"
                  />
                </el-form-item>
              </div>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="visible = false" style="padding: 0 16px;">取消</el-button>
                    <el-button type="primary" @click="submit" style="padding: 0 16px;">确认</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<style lang="scss" scoped>
  .formRow {
    display: flex;
    justify-content: space-between;
  }
  .wwww{
    display: flex;
  }
</style>
